package com.rongxun.hbwallet.ui.activities;

import android.Manifest;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.rongxun.base.MvpActivity;
import com.rongxun.hbwallet.R;
import com.rongxun.hbwallet.constants.IntentKey;
import com.rongxun.hbwallet.constants.SPKey;
import com.rongxun.hbwallet.mvp.contract.ICreateWallet;
import com.rongxun.hbwallet.mvp.model.ApiModel;
import com.rongxun.hbwallet.mvp.presenter.CreateWalletPresenter;
import com.rongxun.utils.ActivityUtils;
import com.rongxun.utils.FlyLog;
import com.rongxun.utils.PreferenceUtils;
import com.rongxun.utils.StatusBarUtil;
import com.rongxun.utils.ToastUtils;
import com.rongxun.utils.wallet.ETHWallet;
import com.rongxun.utils.wallet.WalletDaoUtils;
import com.rongxun.widget.LoadingDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by dwq on 2018/3/14/014.
 * e-mail:lomapa@163.com
 */

public class CreateWalletActivity extends MvpActivity<ICreateWallet.IView, CreateWalletPresenter> implements ICreateWallet.IView, CompoundButton.OnCheckedChangeListener, TextWatcher {
    @BindView(R.id.et_wallet_name)
    EditText  etWalletName;
    @BindView(R.id.et_wallet_pwd)
    EditText  etWalletPwd;
    @BindView(R.id.et_wallet_pwd_again)
    EditText  etWalletPwdAgain;
    @BindView(R.id.btn_create_wallet)
    TextView  btnCreateWallet;
    @BindView(R.id.toolbar_back)
    ImageView mToolbarBack;
    @BindView(R.id.toolbar_title)
    TextView  mToolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar   mToolbar;
    @BindView(R.id.tv_agreement)
    TextView  mTvAgreement;
    private Dialog loadingDialog;

    @NonNull
    @Override
    public CreateWalletPresenter createPresenter() {
        return new CreateWalletPresenter(this, new ApiModel(this));
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_create_wallet;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void showLoading(boolean show) {
        if (show)
            loadingDialog.show();
        else
            loadingDialog.cancel();

    }

    @Override
    public void showError(String msg, String errorCode) {
    }


    @Override
    protected void init() {
        EventBus.getDefault().register(this);
        StatusBarUtil.setToolBarPaddingTop(mToolbar, this);
        mToolbarTitle.setText(getString(R.string.property_drawer_create_wallet));
        mToolbarBack.setVisibility(View.VISIBLE);
        loadingDialog = LoadingDialog.createLoadingDialog(this, getString(R.string.load), false);

        etWalletName.addTextChangedListener(this);
        etWalletPwd.addTextChangedListener(this);
        etWalletPwdAgain.addTextChangedListener(this);
    }

    @Override
    public void release() {
        EventBus.getDefault().unregister(this);
        if (loadingDialog != null)
            loadingDialog.cancel();
    }

    private boolean verifyInfo(String walletName, String walletPwd, String confirmPwd, String pwdReminder) {
        if (WalletDaoUtils.walletNameChecking(walletName)) {
            ToastUtils.showToast(R.string.create_wallet_name_repeat_tips);
            // 同时不可重复
            return false;
        } else if (TextUtils.isEmpty(walletName)) {
            ToastUtils.showToast(R.string.create_wallet_name_input_tips);
            // 同时不可重复
            return false;
        } else if (TextUtils.isEmpty(walletPwd)) {
            ToastUtils.showToast(R.string.create_wallet_pwd_input_tips);
            // 同时判断强弱
            return false;
        } else if (TextUtils.isEmpty(confirmPwd) || !TextUtils.equals(confirmPwd, walletPwd)) {
            ToastUtils.showToast(R.string.create_wallet_pwd_confirm_input_tips);
            return false;
        }
        return true;
    }


    @Override
    public void jumpToWalletBackUp(ETHWallet wallet) {

        getPresenter().saveWallet(PreferenceUtils.getString(SPKey.USERID, "0"), wallet);

    }

    @Override
    public void showSaveWalletResult(ETHWallet wallet) {
        ToastUtils.showToast(getString(R.string.create_wallet_scuess));
        FlyLog.i("createWallet:" + wallet);
        ActivityUtils.startActivityWithAnimotion(this,
                new Intent(this, WalletBackupActivity.class)
                        .putExtra(IntentKey.ETH_WALLET, wallet));
        finish();
    }

    @OnClick({R.id.tv_agreement, R.id.btn_create_wallet, R.id.toolbar_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_agreement:
                break;
            case R.id.btn_create_wallet:
                String walletName = etWalletName.getText().toString().trim();
                String walletPwd = etWalletPwd.getText().toString().trim();
                String confirmPwd = etWalletPwdAgain.getText().toString().trim();
                boolean verifyWalletInfo = verifyInfo(walletName, walletPwd, confirmPwd, "");
                if (verifyWalletInfo) {
                    getPresenter()
                            .checkPermissions(this,
                                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                    Manifest.permission.READ_EXTERNAL_STORAGE);
                }
                break;
            case R.id.toolbar_back:
                onBackPressed();
                break;
        }
    }

    @Override
    public void checkPermissionsSuccessful(String... permissions) {

        getPresenter()
                .createWallet(etWalletName.getText().toString().trim(),
                        etWalletPwd.getText().toString().trim(),
                        etWalletPwdAgain.getText().toString().trim(), "");

    }


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        btnCreateWallet.setEnabled(isChecked);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(ETHWallet wallet) {
        finish();
    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (etWalletName.getText() != null
                && etWalletPwd.getText() != null
                && etWalletPwd.getText().toString().length() >= 8
                && etWalletPwdAgain.getText() != null) {
            if (!btnCreateWallet.isEnabled()) {
                btnCreateWallet.setEnabled(true);
                btnCreateWallet.setTextColor(Color.parseColor("#D8B267"));
            }

        } else {
            if (btnCreateWallet.isEnabled()) {
                btnCreateWallet.setEnabled(false);
                btnCreateWallet.setTextColor(Color.WHITE);
            }
        }
    }
}
